﻿using GalaSoft.MvvmLight.Command;
using Siasun.AFC.RtPlatfromGui.UI.ViewModelInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Siasun.AFC.RtPlatfromGui.UI.ViewModel.BaseViewModel
{
    public abstract class PageBaseViewModel:NavigateViewModel, IPageEventVM
    {
        #region 基础属性
        /// <summary>
        /// 异步状态定时器
        /// </summary>
        private Timer _tvmStatusAsynTimer;

        /// <summary>
        /// 发送状态异步定时器次数
        /// </summary>
        public int _sendRunStatusAsynTimes = 0;
        #endregion

        #region Command 命令
        public RelayCommand<object> LoadCommand { get; set; }
        public RelayCommand<object> UnLoadCommand { get; set; }
        public RelayCommand<object> TouchCommand { get; set; }

        #endregion

        #region 构造函数
        public PageBaseViewModel()
        {
            LoadCommand = new RelayCommand<object>((t) => LoadPageInitLayout(t));
            UnLoadCommand = new RelayCommand<object>((t) => UnLoadPageOper(t));
        }
        #endregion

        #region 方法

        #region 初始化
        /// <summary>
        /// 初始化服务等信息
        /// </summary>
        public abstract void InitConfig();

        /// <summary>
        /// 初始化页面信息数据
        /// </summary>
        public abstract void InitLayout();

        /// <summary>
        /// 页面语音提示初始化
        /// </summary>
        public abstract void InitVoice();
        #endregion

        public override void Cleanup()
        {
            //清除数据
        }
        #endregion

        #region 异步定时器
        /// <summary>
        /// 初始化异步定时器
        /// </summary>
        public void InitAsynBaseTimer()
        {
            if (object.Equals(null, this._tvmStatusAsynTimer))
            {
                this._tvmStatusAsynTimer = new Timer();
                this._tvmStatusAsynTimer.AutoReset = false;
                this._tvmStatusAsynTimer.Enabled = false;
                this._tvmStatusAsynTimer.Elapsed += AsynStatusTimer_Tick;
                this._tvmStatusAsynTimer.Interval = 300;
            }
            this._sendRunStatusAsynTimes = 0; //初始化发送次数
        }

        /// <summary>
        /// 卸载异步时间控件
        /// </summary>
        public void UnloadAsynBaseTimer()
        {
            if (!object.Equals(null, this._tvmStatusAsynTimer))
            {
                this._tvmStatusAsynTimer.Stop();
            }
        }

        /// <summary>
        /// 开启定时器
        /// </summary>
        public void AsynStatusTimerStart()
        {
            if (!object.Equals(null, this._tvmStatusAsynTimer))
            {
                this._tvmStatusAsynTimer.Start();
            }
        }

        /// <summary>
        /// 暂停定时器
        /// </summary>
        public void AsynStatusTimerStop()
        {
            if (!object.Equals(null, this._tvmStatusAsynTimer))
            {
                this._tvmStatusAsynTimer.Stop();
            }
        }

        /// <summary>
        /// 设置信息定时器时间间隔
        /// </summary>
        /// <param name="mili"></param>
        public void SetDetailTimerMiliInterval(int mili)
        {
            if (!object.Equals(null, this._tvmStatusAsynTimer)
                && mili > 0 && mili < 30000)
            {
                this._tvmStatusAsynTimer.Stop();
                this._tvmStatusAsynTimer.Interval = mili;
                this._tvmStatusAsynTimer.Start();
            }
        }

        /// <summary>
        /// 获取状态定时器时间间隔(毫秒)
        /// </summary>
        /// <param name="mili"></param>
        public double GetStatusTimerMiliInterval()
        {
            if (!object.Equals(null, this._tvmStatusAsynTimer))
            {
                return this._tvmStatusAsynTimer.Interval;
            }
            return -1;
        }
        #endregion

        #region 定时器方法
        public abstract void AsynStatusTimer_Tick(object sender, ElapsedEventArgs e);
        #endregion

        #region 事件
        public abstract void LoadPageInitLayout(object obj);

        public abstract void UnLoadPageOper(object obj);
        #endregion
    }
}
